'use strict';

const jwt = require('jsonwebtoken');
const camelcaseKeys = require('camelcase-keys');

module.exports = {
  jwt() {
    return jwt;
  },

  verify() {
    const token = this.cookies.get('token');
    const user = jwt.verify(token, this.app.config.jwtSecret);
    return user;
  },

  returnBody(option = {}) {
    let { code, data, message, count } = option;
    code = code || 200;
    data = data || {};
    message = message || '请求成功';
    count = count || 0;
    this.body = {
      data,
      message,
      code,
      count,
    };
  },

  camelcaseKeys(target) {
    return camelcaseKeys(target, {
      deep: true,
    });
  },
};
